package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.domain.inventory.Lotno;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.basic.service.code.LotnoService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "批次号-控制层")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class LotnoController {

	@Autowired
    private LotnoService lotnoService;
	
	private static final String REQUEST_MODEL = "/lotno";
	
	/**
	 * 根据SN生成批次号
	 * @param sn
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "根据SN生成批次号",notes = "根据SN生成批次号")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
	@PostMapping(REQUEST_MODEL + "/createLotnoBySn")
	public Lotno createLotnoBySn(
			@ApiParam(value = "sn", required = true) 
			@RequestParam(required = true) String sn, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
		String loginName = HttpSessionUtil.getLoginName(servletRequest);
		Lotno lotno = lotnoService.createLotnoBySn(sn, loginName);
		return lotno;
	}
	
	/**
	 * 根据【发货单明细】查找批次号
	 * @param gdnDtlId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "根据【发货单明细】查找批次号",notes = "根据【发货单明细】查找批次号")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
	@GetMapping(REQUEST_MODEL + "/getLotnoByGdnDtlId")
	public Lotno getLotnoByGdnDtlId(
			@ApiParam(value = "发货单明细ID", required = true) 
			@RequestParam(required = true) Long gdnDtlId,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
		Lotno lotno = lotnoService.getLotnoByGdnDtlId(gdnDtlId);
		return lotno;
	}
	
	/**
	 * 根据【收货单明细】查找批次号
	 * @param asnDtlId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "根据【收货单明细】查找批次号",notes = "根据【收货单明细】查找批次号")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
	@GetMapping(REQUEST_MODEL + "/getLotnoByAsnDtlId")
	public Lotno getLotnoByAsnDtlId(
			@ApiParam(value = "收货单明细ID", required = true) 
			@RequestParam(required = true) Integer asnDtlId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
		Lotno lotno = lotnoService.getLotnoByAsnDtlId(asnDtlId);
		return lotno;
	}
	
	/**
	 * 根据【调拨单明细】查找批次号
	 * @param transferDtlId
	 * @param type
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "根据【调拨单明细】查找批次号",notes = "根据【调拨单明细】查找批次号")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
	@GetMapping(REQUEST_MODEL + "/getLotnoByTransferDtlId")
	public Lotno getLotnoByTransferDtlId(
			@ApiParam(value = "调拨单明细ID", required = true) 
			@RequestParam(required = true) Long transferDtlId,
			@ApiParam(value = "0=来源、1=目标", required = true) 
			@RequestParam(required = true) Integer type, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
		Lotno lotno = lotnoService.getLotnoByTransferDtlId(transferDtlId, type);
		return lotno;
	}
	
	/**
	 * 根据【调拨中间表明细】查找批次号
	 * @param ifTransferDtlId
	 * @param type
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "根据【调拨中间表明细】查找批次号",notes = "根据【调拨中间表明细】查找批次号")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
	@GetMapping(REQUEST_MODEL + "/getLotnoByIfTransferDtlId")
	public Lotno getLotnoByIfTransferDtlId(
			@ApiParam(value = "调拨中间表明细ID", required = true) 
			@RequestParam(required = true) Integer ifTransferDtlId,
			@ApiParam(value = "0=来源、1=目标", required = true) 
			@RequestParam(required = true) Integer type, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
		Lotno lotno = lotnoService.getLotnoByIfTransferDtlId(ifTransferDtlId, type);
		return lotno;
	}
}
